<template>
  <div class="question_main">
    <div class="main" style="padding-bottom: 80px;">
      <div class="question question-as-normal">
        <div class="banner-tips">
          <div class="tips">
            <div class="tips-left">
              <img src="../../assets/doctor/question.png">
            </div>
            <div class="tips-right">
              <div class="tips-name"> 导诊医生 </div>
              <div class="tips-text"> 请详细描述患者的疾病症状、患病时长、用药情况，需要我提供什么样的帮助。丁香医生为你保障隐私，病历材料仅医生可见 </div>
            </div>
          </div>
        </div>
        <div class="group">
          <div class="group__cells">
            <div class="editor cells editor">
              <div class="cell has_border editor__textarea">
                <div class="cell__bd">
                  <div class="textarea" bgcolor="#fafafa">
                    <textarea id="body" v-model="answer.question" name="textInput" placeholder="请详细描述你的病情" rows="7" required="required" maxlength="500" class="textarea__input needsclick" style="height: auto; color: rgb(26, 26, 26);"/>
                    <div class="textarea__tip">
                      <div class="textarea__counter">
                        <span class="">{{ answer.question.length }}/500</span>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="cell__ft">
                </div>
              </div>
              <div class="editor__upload">
                <div class="uploader">
                  <div class="uploader-list">
                    <div class="uploader-list-item" v-for="(item, key) in answer.imageUrlList">
                      <img :src="item.url" class="uploader-list-item-figure" >
                      <div class="uploader-list-item-status" style="display: none;">
                        <div class="progress">
                          <div class="progress-bar" style="width: 100%;"/>
                        </div>
                      </div>
                      <div class="uploader-list-item-delete" @click="deleteImage(key)">
                        <img src="../../assets/doctor/delete.png">
                      </div>
                    </div>
                    <div class="uploader-list-btn">
                      <div class="uploader-list-add">
                        <input type="file" id="fileUpload" accept="image/png image/jpeg" @change="imageChange"/>
                        <img src="../../assets/doctor/camera.png" >
                        <div class="uploader-list-add-text">图片</div>
                      </div>
                    </div>
                    <div class="uploader-list-description">
                      <span>上传的病历材料仅医生可见</span>
                    </div>
                  </div>
                </div>
                <div class="editor-xuexi-tips" @click="openTip"> 无法上传图片？ </div>
              </div>
            </div>
          </div>
        </div>
        <div class="group">
          <div class="group__cells">
            <div class="price-selector__content">
              <div class="btn_tips">在线问诊仅限复诊，我承诺所提供的信息真实</div>
              <button class="button button_primary" @click="send_question"> 下一步 </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ref, watch, onMounted} from "vue";
import answer from "../../api/answer.js";
import {ElMessage, ElMessageBox} from "element-plus";
import { useRouter, useRoute} from "vue-router";
const router = useRouter()
const route = useRoute()

// onMounted(()=>{
//   answer.imageUrlList = []
//   answer.question = ""
//   let token = sessionStorage.access_token || localStorage.access_token;
//   answer.get_answer_history(token)
//   console.log(answer.imageList)
// })

const imageChange=(event)=>{
  const file = event.target.files[0];
  if (file) {
    answer.imageList.push(event.target.files[0])
    const reader = new FileReader();
    reader.onload = function(e) {
      answer.imageUrlList.push({'url': e.target.result})
    };
    reader.readAsDataURL(file)// 读取文件内容
  }
}

const deleteImage=(index)=>{
  answer.imageUrlList.splice(index, 1)
  answer.imageList.splice(index, 1)
}


const send_question=()=>{
  if (answer.question.length <= 10) {
    ElMessage.error("请至少用10个字以上的内容描述症状")
    return false
  }
  router.push('/patient')
}

const openTip=()=>{
  ElMessageBox.alert('由于第三方或浏览器版本限制，图片可能无法上传。请先提交问题。' +
      '医生回复后，【下载丁香医生App在“我的问诊”内追问并上传图片】，点击下方链接进行下载（若无法下载，请复制链接到浏览器粘贴）' + '\n'
      + 'https://app.dxy.cn/aspirin/index', '请提问后通过丁香医生App上传图片', {
    confirmButtonText: '我知道了',
    center: true,
  })
}
</script>

<style scoped>
.question_main{
  background-color: #f4f4f4;
}

.main{
  width: 768px;
  height: 590px;
  margin: 0 auto;
}
*, li, ol, ul {
  margin: 0;
  padding: 0;
}
.question-as-normal .banner-tips {
  position: relative;
}

.tips {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  padding: 20px 15px;
  background-color: #fff;
}

.tips-left {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  margin-right: 15px;
}

.tips-left img {
  display: block;
  width: 100%;
  height: 100%;
}

.tips-name {
  color: #333;
  font-weight: 500;
  font-size: 15px;
  line-height: 1.13;
}

.tips-text {
  position: relative;
  margin-top: 8px;
  padding: 10px 6px;
  color: hsla(0, 0%, 40%, .8);
  font-size: 15px;
  line-height: 1.5;
  white-space: normal;
  word-break: break-all;
  background-color: #f4f4f4;
}

.tips-text:before {
  position: absolute;
  top: 10px;
  left: -4px;
  display: inline-block;
  width: 9px;
  height: 9px;
  background: #f4f4f4;
  border-radius: 2px;
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  content: " ";
}

.question-as-normal .banner-tips:after {
  position: absolute;
  right: 16px;
  bottom: 0;
  left: 16px;
  box-sizing: border-box;
  height: 1px;
  background-color: #ebebeb;
  -ms-transform: scaleY(.5);
  transform: scaleY(.5);
  content: "";
}

.question-as-normal .group {
  margin-top: 0;
}
.group {
  margin-top: 10px;
}

.question-as-normal .editor {
  box-sizing: border-box;
  overflow: hidden;
}
.cells {
  position: relative;
}

.group__cells .cell {
  padding: 12px 0 0 4%;
}
.cell, .cell.has_border {
  position: relative;
}
.cell {
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  background-color: #fff;
}

.cell__bd {
  flex: 1;
  overflow: hidden;
}
.textarea {
  position: relative;
  width: 100%;
}
.textarea__input {
  display: block;
  width: 100%;
  color: #333;
  font-size: 14px;
  line-height: 20px;
  border: 0;
  resize: none;
}
.textarea__tip {
  position: relative;
  margin-top: 15px;
  font-size: 12px;
  line-height: 13px;
  text-align: right;
}
.textarea__counter {
  display: inline-block;
  color: #ccc;
}
.cell__ft {
  padding-right: 16px;
  color: #999;
  text-align: right;
}
.uploader {
  padding: 4%;
  background-color: #fff;
}
.uploader-list {
  display: flex;
  flex-wrap: wrap;
}
.uploader-list-btn:not(:nth-child(4n)), .uploader-list-item:not(:nth-child(4n)) {
  margin-right: 8px;
}
.uploader-list-btn, .uploader-list-item {
  position: relative;
  box-sizing: border-box;
  width: calc(25% - 6px);
  margin-bottom: 8px;
  border: .5px solid rgba(0, 0, 0, .05);
  border-radius: 4px;
}
.uploader-list-item-figure {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 4px;
}
.uploader-list-item-status {
  position: absolute;
  -webkit-box-align: center;
  align-items: center;
  -webkit-box-pack: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  color: #fff;
  font-size: 12px;
  word-break: break-all;
  background-color: rgba(0, 0, 0, .7);
  opacity: 0;
}
.uploader-list-item-status .progress {
  width: calc(100% - 20px);
  height: 4px;
  overflow: hidden;
  background-color: hsla(0, 0%, 100%, .4);
  border-radius: 2px;
}
.uploader-list-item-status .progress-bar {
  height: 100%;
  background: #fff;
  transition: width .3s;
}
.uploader-list-item-delete {
  position: absolute;
  top: -5px;
  left: -5px;
  z-index: 20;
  width: 33px;
  height: 33px;
  overflow: hidden;
  background: rgba(0, 0, 0, 0);
  border-radius: 50%;
}
.uploader-list-btn:after, .uploader-list-item:after {
  display: block;
  padding-top: 100%;
  content: "";
}
.uploader-list-item-delete img {
  display: block;
  width: 22px;
  height: 22px;
}
.uploader-list-btn:not(:nth-child(4n)), .uploader-list-item:not(:nth-child(4n)) {
  margin-right: 8px;
}
.uploader-list-btn {
  border: none;
}
.uploader-list-btn, .uploader-list-item {
  position: relative;
  box-sizing: border-box;
  width: calc(25% - 6px);
  margin-bottom: 8px;
  border-radius: 4px;
}
.uploader-list-add {
  position: absolute;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  flex-direction: column;
  -webkit-box-align: center;
  align-items: center;
  -webkit-box-pack: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background-color: #fafafa;
}
.uploader-list-add input[type="file"] {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}
.uploader-list-btn:after, .uploader-list-item:after {
  display: block;
  padding-top: 100%;
  content: "";
}
.uploader-list-add img {
  width: 22px;
}
.uploader-list-add-text {
  margin-top: 12px;
  color: #999;
  font-size: 10px;
  line-height: 12px;
}
.uploader-list-description {
  -webkit-box-flex: 1;
  flex: 1;
  align-self: center;
  margin-left: 6px;
  color: #999;
  font-size: 12px;
  line-height: 1.5;
}
.editor-xuexi-tips {
  padding: 0 4% 15px;
  color: #00c792;
  font-size: 12px;
  background-color: #fff;
}
.price-selector__content {
  padding: 20px 4%;
}
.question-as-normal .btn_tips {
  margin-bottom: 20px;
  color: #b3b3b3;
  font-size: 12px;
  line-height: 14px;
  text-align: center;
}
.button_primary {
  color: #fff !important;
  background-color: #00c792;
}
.button, .button:after {
  box-sizing: border-box;
}
.button {
  position: relative;
  display: block;
  width: 100%;
  height: 46px;
  font-weight: 500;
  font-size: 17px;
  line-height: 46px;
  text-align: center;
  text-decoration: none;
  border: none;
  border-radius: 23px;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
button, input, textarea {
  outline: none;
}
button, input, textarea {
  font: inherit;
}
.button_primary:after {
  border: none;
}
.button:after {
  position: absolute;
  top: 0;
  left: 0;
  width: 200%;
  height: 200%;
  border: 1px solid rgba(0, 0, 0, .3);
  border-radius: 46px;
  -webkit-transform: scale(.5);
  -ms-transform: scale(.5);
  transform: scale(.5);
  -webkit-transform-origin: 0 0;
  -ms-transform-origin: 0 0;
  transform-origin: 0 0;
  content: " ";
}
.button, .button:after {
  box-sizing: border-box;
}
</style>